/**
 * 基于Zepto的左右区域滚动
 * 可定义是否自动，间隔时间，速度
 * 2015-11-5 by zdy(intval@163.com)
 */
;(function($){

  $.fn.transformX = function(options){

      // 默认参数
      var opt = $.extend({

          speed: 0.2

      }, options || {});

      // 返回this，维持链式操作
      return this.each(function(){

          // 预缓存DOM对象
          var oF = $(this);
          var _ww = 0;
          oF.find('a').each(function(){
              _ww += $(this).width();
          });

          var _w = $(window).width();
          if(_ww <= _w) return false;

          var _n = Math.floor(_ww / _w);

          oF.parent().width(_ww);

          var forRun = document.querySelector('#'+oF.attr('id'));
          var startX = 0, startY = 0, moveX = 0, moveY = 0;

          forRun.addEventListener('touchstart', function(ev){ 

              startX = ev.touches[0].pageX;
              startY = ev.touches[0].pageY;

          }, false);

          forRun.addEventListener('touchmove', function(ev){

              moveX = ev.touches[0].pageX;
              moveY = ev.touches[0].pageY;
              
          }, false);

          forRun.addEventListener('touchend', function(ev){

            var endX = moveX - startX, endY = moveY- startY;
              
                if(Math.abs(endX) > 20 && Math.abs(endX) > Math.abs(endY)){

                    var _rs = endX + getTransX();
                    var _lit = 0 - _w * (_n - 1) - (_ww % _w);

                    if(_rs > 0){
                        _rs = 0;
                    }else if(_rs < _lit){
                        _rs = _lit;
                    }
                    var _kernel = kernel();
                    forRun.style.cssText = _kernel + 'transition:all '+ opt.speed +'s linear;'+ _kernel +'transform:translate3d('+ _rs +'px, 0px, 0px);';
                }

          }, false);

          function getTransX(){

              var _kernel = kernel();
              var _css = getStyles(), _transform = _css['transform'],  _tmp = [0, 0];
              if(_css !== false){
                 
                  if(_transform === undefined) _transform = _css[_kernel + 'transform'];
                  var _tmp = _transform.split('(');
              }
              return parseInt(_tmp[1], 10);
          }

          function kernel(){
            var _style = document.createElement('div').style;
                var _kernel = ['-webkit-', '-moz-', '-o-', '-ms-'];
                for(var i in _kernel){
                    var _v = _kernel[i] + 'transform';
                    if(_style[_v] !== undefined) return _kernel[i];
                }
                return '';
          }

          function getStyles(){
              var txt = forRun.style.cssText;
              if(txt.length > 0){
                  txt = txt.replace(/; /g, ';').replace(/: /g, ':').replace(/, /g, ',');
                  var arr = txt.split(';'), _json = {};
                  for(var i in arr){
                      if(arr[i].length > 0){
                          var _tmp = arr[i].split(':');
                          _json[_tmp[0]] = _tmp[1];
                      }
                  }
                  return _json;
              }
              return false;
        }
    });
  };
})(Zepto);